package com.xxgc.eow.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
//import com.xxgc.eow.utils.CosFile;
import com.xxgc.eow.utils.CosFile;
import com.xxgc.eow.utils.CosUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import javax.xml.crypto.Data;

import com.xxgc.eow.result.Result;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.xxgc.eow.service.ICompanyService;
import com.xxgc.eow.entity.Company;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 企业表 前端控制器
 * </p>
 *
 * @author Mr.Abel
 * @since 2023-05-21
 */
@Tag(name = "企业表")
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Resource
    private ICompanyService companyService;

    /**
     * 查询所有接口
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "查询所有接口", description = "查询企业表所有数据")
    @SaIgnore
    @GetMapping
    public Result<List<Company>> findAll() {
        List<Company> allCompany = companyService.getAllCompany();
        return Result.ok(allCompany);
    }

    /**
     * 根据id查询数据接口
     * @param companyId
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "根据企业id查询数据接口", description = "根据企业id查询")
    @Parameters({
            @Parameter(name = "companyId",description = "根据企业id查询",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/{companyId}")
    public Result<Company> findOne(@PathVariable Integer companyId) {
        Company company = companyService.getAllById(companyId);
        return Result.ok(company);
    }


    /**
     * 分页查询接口
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "分页模糊查询接口", description = "传入keyWord 分页模糊查询，不传分页查询所有")
    @Parameters({
            @Parameter(name = "pageNum",description = "页码",example="1",required = true,in=ParameterIn.QUERY),
            @Parameter(name = "pageSize",description = "每页多少条",example="10",required = true,in=ParameterIn.QUERY),
            @Parameter(name = "keyWord",description = "关键字",in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/page")
    public Result<List<Company>> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam(required = false,defaultValue = "") String keyWord) {
        List<Company> companyList = companyService.getCompany(pageNum, pageSize, keyWord);
        return Result.ok(companyList);
    }
    /**
     * 查询企业总数
     * @param keyWord
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "模糊查询企业总数", description = "传入keyWord 模糊查询，不传查询所有")
    @Parameters({
            @Parameter(name = "keyWord",description = "关键字",in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/count")
    public Result<List<Company>> findPage(@RequestParam(required = false,defaultValue = "") String keyWord) {
        if(companyService.getCompany(keyWord).size() == 0){
            return Result.error(123,"未查询到相关信息","");
        }
        return Result.ok(companyService.getCompany(keyWord));
    }


    /**
     * 新增和更新接口
     * @param
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "新增和更新接口", description = "传入id为更新,不传为新增,时间和有效字段均不填")
    @SaIgnore
    @PostMapping("addAndupdate")
    public Result save(@RequestBody Company company){
        if(company.getCreateTime() == null){
            company.setCreateTime(LocalDateTime.now());
        }
        if(company.getCompanyId() == null){
            companyService.addCompany(company);
        }else{
            companyService.updatCompany(company);
        }
        return Result.ok();
    }
    /**
     *
     * @param
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "上传头像接口", description = "上传头像接口")
    @SaIgnore
    @PostMapping("addImg")
    public Result addimg(@RequestBody MultipartFile myFile) throws IOException {
//        String headUrl = System.currentTimeMillis() + myFile.getOriginalFilename().substring(myFile.getOriginalFilename().indexOf("."));
//        File file = new File(headUrl);
//        FileUtils.copyInputStreamToFile(myFile.getInputStream(), file);
//        CosFile.upload("/images/companyUrl/" + headUrl, file);
//        String url = file.getName().substring(0,file.getName().indexOf("."));
//        if (file.exists()) {
//            file.delete();
//        }
//        return Result.ok(CosFile.getFile("/images/companyUrl/"+url).get(0));
        String url = CosUtils.uploadFile(myFile,"/images/companyUrl/");
        return Result.ok(url);
    }


    /**
     * 删除接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "删除接口", description = "通过id删除")
    @Parameters({
            @Parameter(name = "id",description = "组件id",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/del")
    public Result delete(@Parameter Integer id) {
        Company company = new Company();
        company.setCompanyId(id);
        company.setDestroyTime(LocalDateTime.now());
        company.setTip(1);
        companyService.delComany(company);
        return Result.ok();
    }

    /**
     * 批量删除接口
     * @param ids
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "批量删除接口", description = "批量删除接口")
    @SaIgnore
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        for (Integer id : ids) {
            Company company =new Company();
            company.setCompanyId(id);
            company.setDestroyTime(LocalDateTime.now());
            company.setTip(1);
            companyService.delComany(company);
        }
        return Result.ok();
    }
}